<?php if( ! defined('BASEPATH')) exit('No direct script access allowed'); 

class Com_model extends Model
{
	var $uri_segnum = 2;
	function Com_model()
	{
		parent::Model();
	}
	
	function get_top_product($n=10,$c=0,$att='')
	{
		$sql = 'SELECT p.id,p.title,p.title_color,p.summary,p.small_pic_path,p.big_pic_path,p.pNum,p.market_price ,(SELECT cat_name FROM '.$this->db->dbprefix.'product_catalog WHERE queue = p.catalog_id) AS cat_name FROM '.$this->db->dbprefix."product AS p WHERE p.is_trash = 0";
		if($c > 0) $sql .= " AND p.catalog_id like '%,".$c.",%'";
		if($att) $sql .= " AND p.att_id like '%,".$att.",%'";
		$sql .= ' ORDER BY p.seqorder DESC,p.id DESC';
		$sql .= ' LIMIT 0,'.$n;
		return $this->common_model->get_records($sql);
	}
	
	function get_top_news($n=10,$c=0)
	{
		$sql = 'SELECT id,title,title_color,summary,pic_path,create_date,source,hits,author FROM '.$this->db->dbprefix."news WHERE is_trash = 0";
		if($c > 0) $sql .= " AND catalog_id like '%,".$c.",%'";
		$sql .= ' ORDER BY seqorder DESC,id DESC';
		$sql .= ' LIMIT 0,'.$n;
		return $this->common_model->get_records($sql);
	}
	
	function get_link($n=0)
	{
		$sql = 'SELECT id,title,hplink,pic_path FROM '.$this->db->dbprefix."link";
		$sql .= ' ORDER BY seqorder DESC,id DESC';
		if($n > 0) $sql .= ' LIMIT 0,'.$n;
		return $this->common_model->get_records($sql);
	}
	
	function parse_ncatalog($id)
	{
		static $ncatalog_arr=array();
		$sql = 'SELECT c.id,c.cat_name,c.pic_path,c.deep_id,c.is_has_chd,c.parent_id,(SELECT COUNT(*) FROM '.$this->db->dbprefix."news WHERE instr(catalog_id,c.queue) > 0) AS num FROM ".$this->db->dbprefix.'news_catalog AS c WHERE 1 = 1';
		$sql .= ' AND c.parent_id = '.$id;
		$sql .= ' ORDER BY c.seqorder DESC,c.id DESC';
		$catalog = $this->common_model->get_records($sql);
		foreach($catalog as $row)
		{
			$ncatalog_arr[]=$row;
			if($row->is_has_chd==1)
			{
				$this->parse_ncatalog($row->id);
			}
		}
		return $ncatalog_arr;
	}
	
	function parse_pcatalog($id)
	{
		static $pcatalog_arr=array();
		$sql = 'SELECT c.id,c.cat_name,c.pic_path,c.deep_id,c.is_has_chd,c.parent_id,(SELECT COUNT(*) FROM '.$this->db->dbprefix."product WHERE instr(catalog_id,c.queue) > 0) AS num FROM ".$this->db->dbprefix.'product_catalog AS c WHERE 1 = 1';
		$sql .= ' AND c.parent_id = '.$id;
		$sql .= ' ORDER BY c.seqorder DESC,c.id DESC';
		$catalog = $this->common_model->get_records($sql);
		foreach($catalog as $row)
		{
			$pcatalog_arr[]=$row;
			if($row->is_has_chd==1)
			{
				$this->parse_pcatalog($row->id);
			}
		}
		return $pcatalog_arr;
	}
	
	function get_article($q_id)
	{
		$sql = 'SELECT title,pic_path,summary,content FROM '.$this->db->dbprefix."article WHERE q_id = ?";
		return $this->common_model->get_record($sql,array($q_id));
	}
	
	function get_flash()
	{
		$this->db->select('title,hplink,pic_path');
		$this->db->order_by('seqorder','desc');
		$this->db->order_by('id','desc');
		$query=$this->db->get_where('flash',array('is_trash'=>0));
		return $query->result();
	}
	
	function get_nav($arr)
	{
		$defarr = array('nav_str'=>'','nav_title'=>'');
		foreach($arr as $k=>$v)
		{
			$$k = $v;
		}
		unset($arr);
		if( ! isset($catalog_id) || !isset($nav_title) || ! isset($base) || ! isset($tb)) return $defarr;
		$str='';
		$base = rtrim($base,'/').'/';
		if(strpos($catalog_id,',') !== FALSE)
		{
			$catalog_id = trim($catalog_id,',');
			$query = $this->common_model->get_records('SELECT cat_name,id FROM '.$this->db->dbprefix.$tb.' WHERE id in('.$catalog_id.')');
			foreach($query as $row)
			{
				$str.=' &gt; <a href="'.site_url($base.$row->id).'">'.$row->cat_name.'</a>';
			}
			$str .= ' &gt; '.$nav_title;
			return array('nav_str'=>$str,'nav_title'=>$nav_title);
		}
		elseif($catalog_id > 0)
		{
			$query = $this->common_model->get_record('SELECT queue FROM '.$this->db->dbprefix.$tb.' WHERE id = ?',array($catalog_id));
			if( ! $query) return $defarr;
			$queue = $query->queue;
			$queue = trim($queue,',');
			
			$query = $this->common_model->get_records('SELECT cat_name,id FROM '.$this->db->dbprefix.$tb.' WHERE id in('.$queue.')');
			$i = 1;
			foreach($query as $row)
			{
				if(count($query) == $i)
				{
					$str.=' &gt; '.$row->cat_name;
					$nav_title = $row->cat_name;
				}
				else
				{
					$str.=' &gt; <a href="'.site_url($base.$row->id).'">'.$row->cat_name.'</a>';
				}
				$i += 1;
			}
			return array('nav_str'=>$str,'nav_title'=>$nav_title);
		}
		else
		{
			$str.=' &gt; <a href="'.site_url($base).'">'.$nav_title.'</a>';
			return array('nav_str'=>$str,'nav_title'=>$nav_title);
		}
	}
	
	function floatim()
	{
		$data=array(
			'im_catalog'=>$this->db->get('imessage_catalog')->result(),
			'im_item'=>$this->db->get('imessage')->result()
		);
		return $this->load->view(TPL_FOLDER.'floatim',$data,true);
	}
	
	function content_link($id,$c,$t,$b)
	{
		$arr = array('prev'=>'没有了','next'=>'没有了');
		if( ! $id || !$t) return $arr;
		$sql = 'SELECT id,title FROM '.$this->db->dbprefix.$t.' WHERE is_trash = 0';
		if($c) $sql .= " AND catalog_id LIKE '%{$c}%'";
		$sql .= ' ORDER BY seqorder DESC,id DESC';
		$query = $this->db->query($sql);
		$b = rtrim($b,'/').'/';
		$i = 0;
		$rs = $query->result();
		foreach($rs as $row)
		{
			if($row->id == $id)
			{
				if(($i-1) >= 0 && $prev = $query->row($i-1))
				{
					$arr['prev'] = '<a href="'.site_url($b.$prev->id).'" title="'.$prev->title.'">'.strcut($prev->title,48).'</a>';
				} 
				if(($i+1) < count($rs) && $next = $query->row($i+1))
				{
					$arr['next'] = '<a href="'.site_url($b.$next->id).'" title="'.$next->title.'">'.strcut($next->title,48).'</a>';
				} 
				break;
			}
			$i++;
		}
		return $arr;
	}
}
?>